/**
 * Created by dmrj on 2016/4/27.
 */
define(['jquery', 'appMessageBox', 'appConst'], function ($, msgBox, appConst) {
    return {
        search: function (url, callback, errCallBack) {
            $.ajax({
                url: url,
                type: 'POST',
                success: function (data) {
                    if (data.code == 0) {
                        msgBox.errorBox(data.message);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error: function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        },
        update: function (url, params, callback, errCallBack) {
            $.ajax({
                url:url,
                type:"POST",
                contentType: "application/json",
                data:JSON.stringify(params),
                dataType:"json",
                success:function (data){
                    if (data.code == 0) {
                        var errorTip = data.message != null ? $.i18n.prop(data.message, [$.i18n.prop('MENU_SYSTEM_ALARMRULE')]) : $.i18n.prop('GLOBAL_UPDATE_FAIL', $.i18n.prop('MENU_SYSTEM_ALARMRULE'));
                        msgBox.errorBox(errorTip);
                        if (typeof errCallBack == 'function') {
                            errCallBack();
                        }
                    } else {
                        var success = {
                            content: $.i18n.prop('PROJECT_UPDATE_SUCCESS'),
                            type: "success",
                            clean: true
                        };
                        msgBox.show(success);
                        if (typeof callback == 'function') {
                            callback(data.body);
                        }
                    }
                },
                error:function (e) {
                    if (e.status == 401) {
                        msgBox.sessionFailureBox();
                    }
                    if (e.status == 500) {
                        msgBox.serverFailureBox();
                    }
                    if (e.status == 404) {
                        msgBox.pathFailureBox();
                    }
                    if (typeof errCallBack == 'function') {
                        errCallBack();
                    }
                }
            });
        }
    }
});